//
//  HEServicePackDetailsViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/12/9.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEServicePackDetailsViewController.h"
#import "HEHealthManageModel.h"
#import "HEIMChatViewController.h"

@interface HEServicePackDetailsViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;
// 约束参照视图,也是容器视图
@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) UILabel *titleLb;
@property (nonatomic, strong) UITextView *contentLb;

@end

@implementation HEServicePackDetailsViewController
 

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"服务包详请";
     
    [self initUI];
}

- (void)initUI {
    
    // 1. 添加scrollView
    [self.view addSubview:self.scrollView];
    // 2. 添加参照视图
    [self.scrollView addSubview:self.contentView];
 
    // 商品图片
    UIImageView *_goodsimgView = [UIImageView new];
    _goodsimgView.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView addSubview:_goodsimgView];
    [_goodsimgView sd_setImageWithURL:[NSURL URLWithString: _model.pic] placeholderImage: IMAGE_NAMED(@"noImage")];

    // 姓名
    _titleLb = [UILabel new];
    _titleLb.text = _model.product_name;
    _titleLb.font = DYBlodFont(18);
    _titleLb.numberOfLines = 3;
    [self.contentView addSubview:_titleLb];
     
    // middle
    UILabel *timeLb = [UILabel new];
    timeLb.text = [NSString stringWithFormat:@"购买日期：%@", [NSDate timestampToDate: _model.create_date formaterMode:NSDateFormaterModeDay]];
    timeLb.font = DYBlodFont(16);
    timeLb.textColor = CNavDargGrayColor;
    [self.contentView addSubview: timeLb];
 
    [_goodsimgView mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.equalTo(self.contentView).mas_offset(15);
       make.left.equalTo(self.contentView).mas_offset(15);
       make.width.height.mas_offset(90);
    }];
    _goodsimgView.layer.masksToBounds = YES;
    _goodsimgView.layer.cornerRadius = 8;
     
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.equalTo(self.contentView).mas_offset(15);
       make.left.equalTo(_goodsimgView.mas_right).mas_offset(10);
       make.right.equalTo(self.contentView).mas_offset(-10);
    }];
    
    [timeLb mas_makeConstraints:^(MASConstraintMaker *make) {
       make.bottom.equalTo(_goodsimgView.mas_bottom).mas_offset(0);
       make.left.equalTo(_goodsimgView.mas_right).mas_offset(10);
       make.right.equalTo(self.contentView).mas_offset(-10);
    }];
    
    
    // 服务包简介
    UILabel *abourtLb = [UILabel new];
    abourtLb.text = @"服务包简介";
    abourtLb.font = DYBlodFont(18);
    [self.contentView addSubview: abourtLb];
    
    [abourtLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_goodsimgView.mas_bottom).mas_offset(15);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset(-15);
    }];
    
    UILabel *descLb = [UILabel new];
    descLb.text = _model.desc;
    descLb.font = DYFont(16);
    descLb.textColor = CNavDargGrayColor;
    descLb.numberOfLines = 0;
    [self.contentView addSubview: descLb];
    
    [descLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(abourtLb.mas_bottom).mas_offset(10);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset(-15);
    }];
    
    
    // 服务包内容
    UILabel *serCntLb = [UILabel new];
    serCntLb.text = @"服务包内容";
    serCntLb.font = DYBlodFont(18);
    [self.contentView addSubview: serCntLb];
    
    [serCntLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(descLb.mas_bottom).mas_offset(15);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset(-15);
    }];
    
     
    // 描述
    _contentLb = [UITextView new];
    _contentLb.attributedText = _model.KdetailMobileHtml;
    _contentLb.editable = NO;
    _contentLb.scrollEnabled = NO;
    _contentLb.textColor = CNavLightBlackColor;
    _contentLb.font = DYFont(15);
    _contentLb.backgroundColor = UIColor.clearColor;
    [self.contentView addSubview: _contentLb];
 
    [_contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(serCntLb.mas_bottom).mas_offset(5);
        make.left.equalTo(self.contentView).mas_offset(10);
        make.right.equalTo(self.contentView).mas_offset(-10);
    }];
     
 
    // 联系健康管理师
    QMUIFillButton *agreeBtn = [[QMUIFillButton alloc] initWithFillColor:CNavBgColor titleTextColor:CNavBgFontColor];
    agreeBtn.titleLabel.font = DYBlodFont(16);
    [agreeBtn setTitle:@"联系健康管理师" forState:UIControlStateNormal];
    [self.view addSubview:agreeBtn];
    [agreeBtn addTarget:self action:@selector(toHealthClick) forControlEvents:UIControlEventTouchUpInside];
 
    [agreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_contentLb.mas_bottom).mas_offset(20);
        make.centerX.equalTo(self.contentView);
        make.left.equalTo(self.contentView).mas_offset(30);
        make.right.equalTo(self.contentView).mas_offset(-30);
        make.height.mas_offset(45);
        make.bottom.equalTo(self.contentView).mas_offset(-30);
    }];
}

- (void)toHealthClick {
    
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KgetOneHealDoctor parameters:@{} success:^(id  _Nonnull responseObject) {
        NSLog(@"response：  %@", [responseObject mj_JSONString]);
        HEResponse *response = [HEResponse mj_objectWithKeyValues:responseObject];

        HEHealthManageModel *model = [HEHealthManageModel mj_objectWithKeyValues: response.data];
        
        if (response.success && model != nil) { 
            HEIMKitInfo *doc = [HEIMKitInfo new];
            doc.infoId = model.doctor_id;
            doc.showName = @"健康管理师";
            doc.avatarUrlString = nil;
            doc.avatarImage = IMAGE_NAMED(@"hmang");
            
            UserInfo *user = [[UserManager sharedUserManager] getUserInfo];
            HEIMKitInfo *owner = [HEIMKitInfo new];
            owner.infoId = user.id;
            owner.showName = user.realname;
            owner.avatarUrlString = user.avatar;
            owner.avatarImage = IMAGE_NAMED(@"defualt_icon");
            
            [[HEIMKit sharedKit] registeUserInfos: @[doc, owner]];
            
            HEIMChatViewController *vc = [HEIMChatViewController new];
            vc.docId = model.doctor_id;
            [self.navigationController pushViewController:vc animated:YES];
        }else {
            [Dialog toastBottom:@"当前排队人数过多,请稍后再试~"];
        }
        
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    // 布局子控件
    // 设置scrollView约束
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(ZXNavBarHeight, 0, 0, 0));
    }];
    
    // 设置参照视图的约束
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
//        make.height.greaterThanOrEqualTo(@0.0f);
    }];
}
  
#pragma mark - getters
// scrollView
- (UIScrollView *)scrollView {
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.backgroundColor = KWhiteColor;
    }
    return _scrollView;
}

// 约束参照视图
- (UIView *)contentView {
    if (_contentView == nil) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = KWhiteColor;
    }
    return _contentView;
}

@end
